//
//  LessionViewController.m
//  VideoPic
//
//  Created by apple on 16/3/23.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "LessionViewController.h"
#import "NetworkingHandle.h"
#import "ScrollWithImage.h"
#import "ZYNTableViewCellForRecommendSong.h"
#import "ZYNTableViewCellForNewSong.h"
#import "ZYNTableViewCellForSelection.h"
#import "ZYNModelForHotSongs.h"
#import "ZYNViewControllerForPlayBase.h"
#import "ZYNViewControllerForHotSongListDetail.h"

@interface LessionViewController ()<UITableViewDataSource, UITableViewDelegate, ZYNCollectionViewCellForRecommendSongDelegate, ZYNTableViewCellForSelectionDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrForData;
@property (nonatomic, strong) NSMutableArray *arrForPic;
@property (nonatomic, retain) NSMutableArray *arrForHeader;
@property (nonatomic, strong) NSMutableArray *arrForAllData;
@property (nonatomic, retain) NSMutableArray *arrForHotSongs;
@property (nonatomic, retain) NSMutableArray *arrForLove;
@end

@implementation LessionViewController
- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self handleData];
    [self createTableView];
    
}
- (void)handleData {
    self.arrForPic = [NSMutableArray array];
    self.arrForData = [NSMutableArray array];
    self.arrForHeader = [NSMutableArray array];
    self.arrForAllData = [NSMutableArray array];
    self.arrForHotSongs = [NSMutableArray array];
    self.arrForLove = [NSMutableArray array];
    [NetworkingHandle handleWorkData:@"http://api.dongting.com/frontpage/frontpage?v=v8.4.0.2016022715&f=f508&s=s200&version=0" AndBody:nil value:nil handleBlock:^(id result) {
        NSLog(@"%@", result);
        [[result objectForKey:@"data"] objectAtIndex:2];
        [[result objectForKey:@"data"] objectAtIndex:3];
        [[result objectForKey:@"data"] objectAtIndex:4];
        NSMutableArray *array = [NSMutableArray array];
        [array addObject:[[result objectForKey:@"data"] objectAtIndex:0]];
        [array addObject:[[result objectForKey:@"data"] objectAtIndex:2]];
        [array addObject:[[result objectForKey:@"data"] objectAtIndex:3]];
        [array addObject:[[result objectForKey:@"data"] objectAtIndex:4]];
        
        for (NSDictionary *dic in array) {
            if ([[dic objectForKey:@"name"] isEqualToString:@"海报"]) {
            
                for (NSDictionary *dicPic in [dic objectForKey:@"data"]) {
                    
                    [self.arrForPic addObject:[dicPic objectForKey:@"picUrl"]];
                    if (self.arrForPic.count <= 5) {
                        ScrollWithImage *scroll = [[ScrollWithImage alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 200) images:self.arrForPic url:nil withTimer:3.0];
                        
                        self.tableView.tableHeaderView = scroll;
                    }
                }
                
            }
            if ([[dic objectForKey:@"name"] isEqualToString:@"大家在听"]) {

                [self.arrForHeader addObject:[dic objectForKey:@"name"]];
                
                for (NSDictionary *dicc in [dic objectForKey:@"data"]) {
                  NSMutableArray *array =  [dicc objectForKey:@"songs"];
                    for (NSDictionary *dicSong in array) {
                       
                        ZYNModelForHotSongs *model = [[ZYNModelForHotSongs alloc] init];
                        [model setValuesForKeysWithDictionary:dicSong];
                        [self.arrForData addObject:model];
                        NSLog(@"%@", model.name);
                    }
   
                }
                [self.arrForAllData addObject:self.arrForData];
                
            }
            if ([[dic objectForKey:@"name"] isEqualToString:@"热门歌单"]) {

                [self.arrForHeader addObject:[dic objectForKey:@"name"]];
                for (NSDictionary *mDic in [dic objectForKey:@"data"]) {
                    
                    ZYNModelForHotSongs *model = [[ZYNModelForHotSongs alloc] init];
                    [model setValuesForKeysWithDictionary:mDic];
                    [self.arrForHotSongs addObject:model];
               
                
                } ;
                [self.arrForAllData addObject:self.arrForHotSongs];
            
            }
           NSString *str = [NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]] ;
            
            if ([str isEqualToString:@"2890"]) {
                [self.arrForHeader addObject:[dic objectForKey:@"name"]];
               
                for (NSDictionary *diction in [dic objectForKey:@"data"]) {
                    ZYNModelForHotSongs *model = [[ZYNModelForHotSongs alloc] init];
                    [model setValuesForKeysWithDictionary:diction];
                    [self.arrForLove addObject:model];
                } ;
                [self.arrForAllData addObject:self.arrForLove];
        }

        
        }
        
        [self.tableView reloadData];
    }];
    
}
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.tableHeaderView.backgroundColor = [UIColor lightGrayColor];
  
    [self.tableView registerClass:[ZYNTableViewCellForRecommendSong class] forCellReuseIdentifier:@"pool1"];
    
    [self.tableView registerClass:[ZYNTableViewCellForNewSong class] forCellReuseIdentifier:@"pool2"];
    
    [self.tableView registerClass:[ZYNTableViewCellForSelection class] forCellReuseIdentifier:@"pool3"];
    
}
// 自定义区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    if (section == 2) {
       
        UIView *viewForHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 9)];
        viewForHeader.backgroundColor = [UIColor whiteColor];
       UILabel *labelForHeaderInSection = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, viewForHeader.frame.size.width, viewForHeader.frame.size.height - 10)];
        labelForHeaderInSection.text = [self.arrForHeader objectAtIndex:2];
        
        [viewForHeader addSubview:labelForHeaderInSection];
        return viewForHeader;
        
    } else {
        
        UIView *viewForHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 9)];
        viewForHeader.backgroundColor = [UIColor whiteColor];
        UILabel *labelForHeaderInSection = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, viewForHeader.frame.size.width, viewForHeader.frame.size.height - 10)];
        labelForHeaderInSection.text = [self.arrForHeader objectAtIndex:section];
        
        [viewForHeader addSubview:labelForHeaderInSection];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(viewForHeader.frame.size.width / 4 * 3 + 12, 10, viewForHeader.frame.size.width / 4, viewForHeader.frame.size.height - 10);
        [button setTitle:@"更多 >" forState:UIControlStateNormal];
        button.tag = section;
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(handleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [viewForHeader addSubview:button];
        
        return viewForHeader;
    }
    
}
- (void)handleButtonClick:(UIButton *)button {
    if (button.tag == 0) {
        ZYNViewControllerForPlayBase *listenDetail = [[ZYNViewControllerForPlayBase alloc] init];
        listenDetail.selected = YES;
        [self.navigationController pushViewController:listenDetail animated:YES];
        
    } else {
        ZYNViewControllerForHotSongListDetail *hotSongListDetail = [[ZYNViewControllerForHotSongListDetail alloc] init];
        
        [self.navigationController pushViewController:hotSongListDetail animated:YES];
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 40;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.arrForData.count;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return HEIGHT / 7 * 2;
    
    } if (indexPath.section == 1) {
        
        return HEIGHT / 2;
        
    } else {
        
        return HEIGHT / 4;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ZYNTableViewCellForNewSong *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        [cell passArray:[self.arrForAllData objectAtIndex:indexPath.section]];
        return cell;
        
    } else if(indexPath.section == 1){
        ZYNTableViewCellForRecommendSong *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        [cell passArray:[self.arrForAllData objectAtIndex:indexPath.section]];
        cell.delegate = self;
        
        return cell;

    } else {
        ZYNTableViewCellForSelection *cell = [tableView dequeueReusableCellWithIdentifier:@"pool3"];
        cell.delegate = self;
        [cell passArray:[self.arrForAllData objectAtIndex:indexPath.section]];
        
        return cell;
    }
    
}
- (void)handleDidClick:(ZYNModelForHotSongs *)model {
    NSLog(@"dddd");
    NSLog(@"%@", [model.action objectForKey:@"value"]);
    ZYNViewControllerForPlayBase *detail = [[ZYNViewControllerForPlayBase alloc] init];
    detail.selected = NO;
    detail.strForSongId = [model.action objectForKey:@"value"];
    [self.navigationController pushViewController:detail animated:YES];

}
- (void)handleClick:(ZYNModelForHotSongs *)model {
    
    ZYNViewControllerForPlayBase *detail = [[ZYNViewControllerForPlayBase alloc] init];
    detail.selected = NO;
    detail.strForSongId = [model.action objectForKey:@"value"];
    [self.navigationController pushViewController:detail animated:YES];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
